package controller;

import beans.FileSystem;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Controller
public class Upload {

   @Autowired
   private Redisson redisson;

   @RequestMapping("/upload")
    public @ResponseBody synchronized FileSystem upload(MultipartHttpServletRequest request){
       FileSystem fileSystem = new FileSystem();

       //1.从页面请求中获取上传的文件对象
       MultipartFile file = request.getFile("fname");

       //2.从文件对象中获取文件的原始名称
       String oldFileName = file.getOriginalFilename();

       //3.通过字符串截取的方式，从文件原始名称中获取文件的后缀
       String houzhuiming = oldFileName.substring(oldFileName.lastIndexOf(".") + 1);

       //4.通过redisson获取锁
       //底层源码就是集成了setnx，过期时间等操作
       RLock rLock = redisson.getLock(oldFileName);

       //5.设置过期时间为30s的锁
       rLock.lock(30, TimeUnit.SECONDS);

      /**
       * 把文件从web服务器上传到FastDFS
       */
      try {
         //加载配置文件
         ClientGlobal.initByProperties("config/fastdfs-client.properties");

         //传火箭tracker客户端
         TrackerClient trackerClient = new TrackerClient();

         //通过tracker客户端获取tracker连接服务并返回
         TrackerServer trackerServer = trackerClient.getConnection();

         //声明storage服务
         StorageServer storageServer = null;

         //定义storage客户端
         StorageClient1 client = new StorageClient1(trackerServer, storageServer);

         //定义元信息
         NameValuePair[] list = new NameValuePair[1];
         list[0] = new NameValuePair("fileName",oldFileName);
         String fileId = client.upload_file1(file.getBytes(), houzhuiming, list);
         trackerServer.close();

         //封装数据
         fileSystem.setFileId(fileId);
         fileSystem.setFileName(oldFileName);
         //因为已经上传到FastDFS上，所以可以通过fileId(即为文件路径)来访问文件
         fileSystem.setFilePath(fileId);
         System.out.println(fileSystem);


      } catch (Exception e) {
         e.printStackTrace();
      }  finally {
         //释放锁
         rLock.unlock();
      }
      return fileSystem;
   }

   @Bean
   public Redisson redisson(){
      Config config = new Config();
      //使用单个服务器
      config.useSingleServer().setAddress("redis://192.168.141.129:6379").setDatabase(0);
      RedissonClient client = Redisson.create(config);
      //集群服务器
      //config.useClusterServers().setScanInterval(2000).addNodeAddress("redis://","redis://");
      return (Redisson)client;
   }
}
